package cn.baddonkey.photography.agent.core;

import cn.baddonkey.photography.agent.core.constants.CommonConstants;

import org.slf4j.MDC;

public class PlatformResponse<T> {

    private String status;

    private String message;

    private T content;

    private String traceId;

    private String getThreadTraceId() {
        return MDC.get(CommonConstants.REQUEST_TRACE_ID);
    }

    public PlatformResponse(APIResponseStatus status, T content) {
        if (status == null) {
            throw new RuntimeException(
                    "PlatformResponse init method reported exception. APIResponseStatus is null. Please check your source code.");
        }
        this.status = status.value();
        this.message = status.message();
        this.content = content;
        this.traceId = getThreadTraceId();
    }

    public PlatformResponse(APIResponseStatus status) {
        if (status == null) {
            throw new RuntimeException(
                    "PlatformResponse init method reported exception. APIResponseStatus is null. Please check your source code.");
        }
        this.status = status.value();
        this.message = status.message();
        this.traceId = getThreadTraceId();
    }

    public PlatformResponse(APIResponseStatus status, String message, T content) {
        if (status == null) {
            throw new RuntimeException(
                    "PlatformResponse init method reported exception. APIResponseStatus is null. Please check your source code.");
        }
        this.status = status.value();
        this.message = message;
        this.content = content;
        this.traceId = getThreadTraceId();
    }

    public static PlatformResponse error(APIResponseStatus status, String message) {
        PlatformResponse r = new PlatformResponse();
        r.status = status.value();
        r.message = message;
        return r;
    }

    public static PlatformResponse error(APIResponseStatus status) {
        PlatformResponse r = new PlatformResponse();
        r.status = status.value();
        r.message = status.message();
        return r;
    }

    public static PlatformResponse error(String erroeMsg) {
        PlatformResponse r = new PlatformResponse();
        r.status = APIResponseStatus.U9999.value();
        r.message = erroeMsg;
        return r;
    }

    public static PlatformResponse ok(APIResponseStatus status, String message) {
        PlatformResponse r = new PlatformResponse();
        r.status = status.value();
        r.message = message;
        return r;
    }

    public static PlatformResponse ok() {
        PlatformResponse r = new PlatformResponse();
        r.status = APIResponseStatus.U0001.value();
        r.message = APIResponseStatus.U0001.message();
        return r;
    }

    public static <T> PlatformResponse<T> ok(T content) {
        PlatformResponse<T> r = new PlatformResponse<>();
        r.status = APIResponseStatus.U0001.value();
        r.message = APIResponseStatus.U0001.message();
        r.content = content;
        return r;
    }

    public PlatformResponse resultData(T resultData) {
        this.setContent(resultData);
        this.setTraceId(getThreadTraceId());
        return this;
    }

    private PlatformResponse() {
        this.traceId = getThreadTraceId();
    }

    public APIResponseStatus getStatus() {
        APIResponseStatus[] apiResponseStatus = APIResponseStatus.values();
        if (this.status != null) {
            for (APIResponseStatus temp : apiResponseStatus) {
                if (temp.value().equals(this.status)) {
                    return temp;
                }
            }
        }
        return null;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }
}
